using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 02/04/2010 11:03:48
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CMetasLotacoes
   {
       private string IDT_IND ;
       private string IDT_TPO_MED ;
       private string IDT_LOTACAO ;
       private string SEQ_META ;
       private string IDT_DESC ;
       private string META ;
       private string TOTAL ;

	   /// <sumary>
       /// Construtor da classe metas_lotacoes
       /// <sumary>
       public CMetasLotacoes()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Metas_lotacoes
       /// <sumary>
       public CMetasLotacoes( string IDT_IND, string IDT_TPO_MED, string IDT_LOTACAO, string SEQ_META, string IDT_DESC, string META, string TOTAL )
       {
          this.IDT_IND = IDT_IND;
          this.IDT_TPO_MED = IDT_TPO_MED;
          this.IDT_LOTACAO = IDT_LOTACAO;
          this.SEQ_META = SEQ_META;
          this.IDT_DESC = IDT_DESC;
          this.META = META;
          this.TOTAL = TOTAL;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do indicador
        /// <sumary>
        public string Idt_ind 
		{
		   get
		   {
		       return this.IDT_IND;
		   }
		   set
		   {
		       this.IDT_IND = value;
		   }
		}
     	/// <sumary>
        /// Identificador do tipo de medida
        /// <sumary>
        public string Idt_tpo_med 
		{
		   get
		   {
		       return this.IDT_TPO_MED;
		   }
		   set
		   {
		       this.IDT_TPO_MED = value;
		   }
		}
     	/// <sumary>
        /// Identificador da lotacao do tecnico
        /// <sumary>
        public string Idt_lotacao 
		{
		   get
		   {
		       return this.IDT_LOTACAO;
		   }
		   set
		   {
		       this.IDT_LOTACAO = value;
		   }
		}
     	/// <sumary>
        /// Sequencial da meta
        /// <sumary>
        public string Seq_meta 
		{
		   get
		   {
		       return this.SEQ_META;
		   }
		   set
		   {
		       this.SEQ_META = value;
		   }
		}
     	/// <sumary>
        /// Identificador da descri??o da op??o
        /// <sumary>
        public string Idt_desc 
		{
		   get
		   {
		       return this.IDT_DESC;
		   }
		   set
		   {
		       this.IDT_DESC = value;
		   }
		}
     	/// <sumary>
        /// Valor da meta
        /// <sumary>
        public string Meta 
		{
		   get
		   {
		       return this.META;
		   }
		   set
		   {
		       this.META = value;
		   }
		}
     	/// <sumary>
        /// Total produzido
        /// <sumary>
        public string Total 
		{
		   get
		   {
		       return this.TOTAL;
		   }
		   set
		   {
		       this.TOTAL = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Metas_lotacoes.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_METAS_LOTACOES
			                  ( IDT_IND, IDT_TPO_MED, IDT_LOTACAO, SEQ_META, IDT_DESC, META, TOTAL )
						   values
						      ( :IDT_IND, :IDT_TPO_MED, :IDT_LOTACAO, :SEQ_META, :IDT_DESC, :META, :TOTAL )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_IND",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TPO_MED",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_LOTACAO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("SEQ_META",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_DESC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("META",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("TOTAL",OracleType.Number)); 
				
                command.Parameters["IDT_IND"].Value = this.IDT_IND;
                command.Parameters["IDT_TPO_MED"].Value = this.IDT_TPO_MED;
                command.Parameters["IDT_LOTACAO"].Value = this.IDT_LOTACAO;
                command.Parameters["SEQ_META"].Value = this.SEQ_META;
                command.Parameters["META"].Value = this.META;
                if (String.IsNullOrEmpty(this.IDT_DESC))
                {
                    command.Parameters["IDT_DESC"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["IDT_DESC"].Value = this.IDT_DESC;
                }  
                if (String.IsNullOrEmpty(this.TOTAL))
                {
                    command.Parameters["TOTAL"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["TOTAL"].Value = this.TOTAL;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Metas_lotacoes.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_METAS_LOTACOES set 
			                   IDT_DESC = :IDT_DESC, META = :META, TOTAL = :TOTAL
						   where
						       IDT_IND = :IDT_IND, IDT_TPO_MED = :IDT_TPO_MED, IDT_LOTACAO = :IDT_LOTACAO, SEQ_META = :SEQ_META ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_IND",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_TPO_MED",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_LOTACAO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("SEQ_META",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_DESC",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("META",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("TOTAL",OracleType.Number)); 
				
                command.Parameters["IDT_IND"].Value = this.IDT_IND;
                command.Parameters["IDT_TPO_MED"].Value = this.IDT_TPO_MED;
                command.Parameters["IDT_LOTACAO"].Value = this.IDT_LOTACAO;
                command.Parameters["SEQ_META"].Value = this.SEQ_META;
                command.Parameters["META"].Value = this.META;
                if (String.IsNullOrEmpty(this.IDT_DESC))
                {
                    command.Parameters["IDT_DESC"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["IDT_DESC"].Value = this.IDT_DESC;
                }  
                if (String.IsNullOrEmpty(this.TOTAL))
                {
                    command.Parameters["TOTAL"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["TOTAL"].Value = this.TOTAL;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Metas_lotacoes.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_METAS_LOTACOES", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_IND", DbType.Int32, this.IDT_IND));
            tab.adicionarFiltro(new Coluna("IDT_TPO_MED", DbType.Int32, this.IDT_TPO_MED));
            tab.adicionarFiltro(new Coluna("IDT_LOTACAO", DbType.Int32, this.IDT_LOTACAO));
            tab.adicionarFiltro(new Coluna("SEQ_META", DbType.Int32, this.SEQ_META));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_METAS_LOTACOES order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CMetasLotacoes PesquisaporCodigo(string idt_ind, string idt_tpo_med, string idt_lotacao, string seq_meta, string login)
        {
            CMetasLotacoes obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_METAS_LOTACOES 
                            WHERE IDT_IND = :IDT_IND, IDT_TPO_MED = :IDT_TPO_MED, IDT_LOTACAO = :IDT_LOTACAO, SEQ_META = :SEQ_META";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_IND", DbType.String, idt_ind);
				qy.AdicionarParametro(":IDT_TPO_MED", DbType.String, idt_tpo_med);
				qy.AdicionarParametro(":IDT_LOTACAO", DbType.String, idt_lotacao);
				qy.AdicionarParametro(":SEQ_META", DbType.String, seq_meta);
               
				qy.Executar();
                obj = new CMetasLotacoes( qy.ObterCampo("IDT_IND"), qy.ObterCampo("IDT_TPO_MED"), qy.ObterCampo("IDT_LOTACAO"), qy.ObterCampo("SEQ_META"), qy.ObterCampo("IDT_DESC"), qy.ObterCampo("META"), qy.ObterCampo("TOTAL") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CMetasLotacoes();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_INDIDT_TPO_MEDIDT_LOTACAOSEQ_META() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_IND(string idt_ind)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_METAS_LOTACOES  where IDT_IND = :IDT_IND order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_IND", OracleType.Number));
                command.Parameters["IDT_IND"].Value = idt_ind;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_TPO_MED(string idt_tpo_med)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_METAS_LOTACOES  where IDT_TPO_MED = :IDT_TPO_MED order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_TPO_MED", OracleType.Number));
                command.Parameters["IDT_TPO_MED"].Value = idt_tpo_med;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_LOTACAO(string idt_lotacao)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_METAS_LOTACOES  where IDT_LOTACAO = :IDT_LOTACAO order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_LOTACAO", OracleType.Number));
                command.Parameters["IDT_LOTACAO"].Value = idt_lotacao;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_DESC(string idt_desc)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_METAS_LOTACOES  where IDT_DESC = :IDT_DESC order by IDT_IND ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_DESC", OracleType.Number));
                command.Parameters["IDT_DESC"].Value = idt_desc;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

